package org.hncors.gnssparse;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

import java.util.ArrayList;


public class SelectTemplateDialog extends DialogFragment {
	
	private int mUniqueIdentifier = -1;
	private ArrayList<String> mItemInfoLinkedList;
	private onCustomDialogSelectedListener mOnListener;

	public static SelectTemplateDialog newInstance(
			String title,
			ArrayList<String> itemsList,
			int defaultIndex, 
			int uniqueIdentifier) {
		SelectTemplateDialog pcid = new SelectTemplateDialog();
		Bundle args = new Bundle();
		args.putString("SelectTemplateTitle", title);
		args.putSerializable("MultipleTemplateListData", itemsList);
		args.putInt("SelectTemplateDefault", defaultIndex);
		args.putInt("MultipleTemplateIdentifier", uniqueIdentifier);
		pcid.setArguments(args);
		return pcid;
	}

	public interface onCustomDialogSelectedListener {
		

		public abstract void onCustomDialogSingleSelectedListener(int uniqueIdentifier, int selectIndex, ArrayList<String> data);
	}
	
	@Override
	public Dialog onCreateDialog(Bundle saveInstanceState) {
		@SuppressWarnings("unchecked")
		String title = getArguments().getString("SelectTemplateTitle");
		mItemInfoLinkedList = (ArrayList<String>)getArguments().getSerializable("MultipleTemplateListData");
		int defaultIndex = getArguments().getInt("SelectTemplateDefault");
		mUniqueIdentifier = getArguments().getInt("MultipleTemplateIdentifier");
		
		CustomAlertDialog.Builder builder = new CustomAlertDialog.Builder(getActivity())
					.setTitle(title)
					.setSingleChoiceItems(mItemInfoLinkedList.toArray(new CharSequence[mItemInfoLinkedList.size()]), defaultIndex,
							new DialogInterface.OnClickListener() {
								
								@Override
								public void onClick(DialogInterface arg0, int select) {
									dismiss();
									if (mOnListener != null) mOnListener.onCustomDialogSingleSelectedListener(mUniqueIdentifier, select, mItemInfoLinkedList);							
								}
							});
		 
		 return builder.create();
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mOnListener = (onCustomDialogSelectedListener)activity;
		} catch (ClassCastException e) {
			//throw new ClassCastException(activity.toString() + "must implement onSelectedListener");
			dismiss();
		}
	}
}
